package com.weixin.project.Controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weixin.project.Common.Enum.ReportStatus;
import com.weixin.project.Entity.InformReport;
import com.weixin.project.Entity.User;
import com.weixin.project.Service.ReportService;
import com.weixin.project.Service.UserService;
import com.weixin.project.Util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@RestController
public class ReportController {
    @Autowired
    UserService userService;
    @Autowired
    ReportService reportService;
    @PostMapping("/report/report_question")
    JSONObject reportQuestion(HttpServletRequest request,
                              @RequestBody JSONObject params){
        User currentUser = userService.findCurrentUser(request);
        InformReport record = new InformReport();
        record.setType(1);
        record.setObjectId(params.getInteger("id"));
        record.setText(params.getString("text"));
        record.setCreateTime(new Date());
        record.setReportStatus(ReportStatus.TO_BE_PROCESSED);
        record.setOriginId(currentUser.getId());
        reportService.storeReport(record);
        return ResponseUtil.success();
    }

    @PostMapping("/report/report_video")
    JSONObject reportVideo(HttpServletRequest request,
                              @RequestBody JSONObject params){
        User currentUser = userService.findCurrentUser(request);
        InformReport record = new InformReport();
        record.setType(2);
        record.setObjectId(params.getInteger("id"));
        record.setText(params.getString("text"));
        record.setCreateTime(new Date());
        record.setReportStatus(ReportStatus.TO_BE_PROCESSED);
        record.setOriginId(currentUser.getId());
        reportService.storeReport(record);
        return ResponseUtil.success();
    }

    @GetMapping("/report/get_report_question")
    JSONObject getReportQuestion(HttpServletRequest request,
                                 @RequestParam("page") Integer page){
        User currentUser = userService.findCurrentUser(request);
        page -- ;
        Page<InformReport> reportsPage = reportService.findQuestionReports(page);
        List<InformReport> reports = reportsPage.getContent();
        JSONArray array = new JSONArray();
        for (InformReport report:
            reports) {
            JSONObject obj = new JSONObject();
            obj.put("id", report.getId());
            obj.put("text", report.getText());
            obj.put("question_id", report.getObjectId());
            array.add(obj);
        }
        JSONObject ret = new JSONObject();
        ret.put("questions", array);
        ret.put("all_pages", reportsPage.getTotalPages());
        return ResponseUtil.success("", ret);
    }

    @GetMapping("/report/get_report_video")
    JSONObject getReportVideo(HttpServletRequest request,
                                 @RequestParam("page") Integer page){
        User currentUser = userService.findCurrentUser(request);
        page -- ;
        Page<InformReport> reportsPage = reportService.findVideoReports(page);
        List<InformReport> reports = reportsPage.getContent();
        JSONArray array = new JSONArray();
        for (InformReport report:
                reports) {
            JSONObject obj = new JSONObject();
            obj.put("id", report.getId());
            obj.put("text", report.getText());
            obj.put("video_id", report.getObjectId());
            array.add(obj);
        }
        JSONObject ret = new JSONObject();
        ret.put("videos", array);
        ret.put("all_pages", reportsPage.getTotalPages());
        return ResponseUtil.success("", ret);
    }

    @PostMapping("/report/deny_question_report")
    JSONObject refuseQuestionReport(@RequestBody JSONObject params){
        reportService.refuseReport(params.getInteger("id"));
        return ResponseUtil.success();
    }

    @PostMapping("/report/deny_video_report")
    JSONObject refuseVideoReport(@RequestBody JSONObject params){
        reportService.refuseReport(params.getInteger("id"));
        return ResponseUtil.success();
    }
}
